package com.rimi.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.rimi.bean.UserBean;

public class LoginInterceptor implements HandlerInterceptor {
	public LoginInterceptor(){
		System.out.println("创建登陆拦截器");
	}
	
	// 响应完成后执行
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println(33333333);
	}

	// 在controller执行之后 响应页面之前 执行这段代码
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println(2222222);
	}

	
	// arg2被拦截的对象controller
	// return true 放行 继续向下执行
	// return false 拦截
	// 是在controller之前执行的
	//因为每次被拦截的请求都会被判断session的attribute,如果第一次登陆成功,这个属性会一直存在,所以都会被放行
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(11111);
		UserBean ub = (UserBean) arg0.getSession().getAttribute("ub");
		if (ub != null) {
			return true;
		}else{
			arg1.sendRedirect("getLogin2");
		}
		return false;
	}

}
